package graphics;

import static bogus.core.BogusCore.*;
import hubol.Sprite;

import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GL3;

import owg.util.Calc;
import owg.util.euclidian.Compass;
import owg.util.opengl.ColorF;
import owg.util.opengl.ImmediateModeWrapper;

public class Shape {
public static Sprite rect = new Sprite("rectangle");

	public static void drawRectangle(float x1, float y1, float x2, float y2){
		rect.setOrigin(Compass.NORTHWEST);
		rect.render(0, x1, y1, Math.abs(x2-x1) / 16, Math.abs(y2-y1) / 16, 0);
	}
	
	public static void drawRectangle(boolean width, float x, float y, float w, float h){
		drawRectangle(x, y, x + w, y + h);
	}
	
	public static void drawLine(float x1, float y1, float x2, float y2, float width){
		rect.setOrigin(Compass.WEST);
		rect.render(0, x1, y1, (float)Calc.dist(x1, y1, x2, y2)/16, width/16, (float)Calc.dir(x1, y1, x2, y2));
	}
	
	public static void drawPoint(float x, float y){
		rect.setOrigin(Compass.CENTER);
		rect.render(0, x, y, (float)(1/16.0), (float)(1/16.0), 0);
	}
	
	public static void drawCircle(float x, float y, float radius){
		drawCircle(x, y, radius, 20);
	}
	
	public static void drawCircle(float x, float y, float radius, int steps){
		ImmediateModeWrapper<GL3> im = egl.immediateMode();
		im.glBegin(false, GL2.GL_TRIANGLE_FAN, 2, false, 0, 0);
		im.glVertex2d(x,y);
		for	(int a = 0; a<=steps; a++)
			im.glVertex2d(x+radius*Math.cos(Math.PI*2*(float)a/steps),y+radius*Math.sin(Math.PI*2*(float)a/steps));
		im.glEnd();
	}
	
	
	public static void drawPieChart(float x, float y, float radius, int steps, float angle, float[] values, ColorF... colors){
		float[] percent = new float[values.length];
		float total = 0;
		for (int i=0; i<values.length; i++)
			total += values[i];
		for (int i=0; i<values.length; i++)
			percent[i] = values[i] / total;
		
		ImmediateModeWrapper<GL3> im = egl.immediateMode();
		
		for (int i=0; i<values.length; i++){
			egl.setColor(colors[i]);
			im.glBegin(false, GL2.GL_TRIANGLE_FAN, 2, false, 0, 0);
			im.glVertex2d(x,y);
			int tot = (int)Math.max(6, Math.ceil(steps * percent[i]));
			for(int a = 0; a<=tot; a++){
				im.glVertex2d(x+radius*Math.cos(Math.PI*angle),y+radius*Math.sin(Math.PI*angle));
				if (a < tot)
					angle += ((1.0 / (float)tot) * percent[i]) * 2.0;
			}
			im.glEnd();
		}
	}
	
	public static void drawRingChart(float x, float y, float radius, int steps, float angle, float[] values, ColorF... colors){
		int amnt = values.length;
		float[] percent = new float[amnt];
		float total = 0;
		for (int i=0; i<amnt; i++)
			total += values[i];
		for (int i=0; i<amnt; i++)
			percent[i] = values[i] / total;
		
		float totalpercent = 1;
		for (int i=amnt - 1; i>=0; i--){
			egl.setColor(colors[i]);
			drawCircle(x, y, (float)(Math.sqrt(totalpercent * Math.pow(radius, 2) * Math.PI) / Math.PI), (int)Math.ceil((float)steps / (float)amnt));
			totalpercent -= percent[i];
		}
	}
	
}
